import pandas as pd

def createSeries():
    data = [32.5, 32.6, 33, 32.1, 36]
    index = ["2023-07-01", "2023-07-02", "2023-07-03", "2023-07-04", "2023-07-05"]

    series_1 = pd.Series(data)
    print(series_1)

    series_2 = pd.Series(data, dtype=float)
    print(series_2)

    series_3 = pd.Series(data=data)
    print(series_3)

    series_4 = pd.Series(data=data, dtype=float)
    print(series_4)

    series_5 = pd.Series(data, index)
    print(series_5)

    series_6 = pd.Series(data=data, index=index)
    print(series_6)

    series_7 = pd.Series(data=data, index=index, dtype=float)
    print(series_7)


def splitSeries():
    data = [32.5, 32.6, 33, 32.1, 36]
    index = ["2023-07-01", "2023-07-02", "2023-07-03", "2023-07-04", "2023-07-05"]

    series = pd.Series(data=data, index=index)

    print()
    split_02 = series['2023-07-02']
    print(split_02)

    split_0204 = series['2023-07-02':'2023-07-04']
    print(split_0204)

    split_0304 = series[1: 3]
    print(split_0304)


def getSeriesIndexOrValue():
    data = [32.5, 32.6, 33, 32.1, 36]
    index = ["2023-07-01", "2023-07-02", "2023-07-03", "2023-07-04", "2023-07-05"]

    series = pd.Series(data=data, index=index)

    index = series.index
    print(index)

    values = series.values
    print(values)


def seriesAutoDataProcess():
    """
    利用索引合并数据
    :return:
        i1      NaN
        i2    220.0
        i3    330.0
        i4    440.0
        i5      NaN
        dtype: float64
    """
    index_1 = ['i1', 'i2', 'i3', 'i4']
    data_1 = [100, 200, 300, 400]

    index_2 = ['i2', 'i3', 'i4', 'i5']
    data_2 = [20, 30, 40, 50]

    series_1 = pd.Series(data=data_1, index=index_1)
    series_2 = pd.Series(data=data_2, index=index_2)

    print(series_1 + series_2)


def seriesDataProcess():
    data = [11, 22, 33, 44, 66]
    index = ['i1', 'i2', 'i3', 'i4', 'i6']
    reindex = ['i0', 'i1', 'i2', 'i3', 'i4', 'i5', 'i6', 'i7']

    series = pd.Series(data=data, index=index)
    print(series)

    # 对Series对象重新设置索引，非交集设置为NaN
    series_reindex = series.reindex(reindex)
    print(series_reindex)

    # 对Series对象重新设置索引，非交集设置为fill_value的值(本例中设置为0)
    series_reindex_fill_value = series.reindex(reindex, fill_value=0)
    print(series_reindex_fill_value)

    # 向前填充
    series_reindex_ffill = series.reindex(reindex, method="ffill")
    print(series_reindex_ffill)

    # 向后填充
    series_reindex_bfill = series.reindex(reindex, method="bfill")
    print(series_reindex_bfill)


if __name__ == '__main__':
    createSeries()
    splitSeries()
    getSeriesIndexOrValue()
    seriesAutoDataProcess()
    seriesDataProcess()
